﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Lib.Extraction
{
    class DataPatternExtract
    {
        string _dataPattern = "";
        public string DataPattern { get { return _dataPattern; } set { _dataPattern = value; } }

        public DataPatternExtract()
        {

        }

        /// <summary>
        /// Initiate extracting module with Data Pattern
        /// </summary>
        /// <param name="_pattern">Regex to get the data</param>
        public DataPatternExtract(string _pattern)
        {
            DataPattern = _pattern;
        }

        public static void DataToXML(string _url, string _urlName, string _content, string rootURL, string _pattern, string fPath, string CategoryID)
        {
            XmlDocument xdoc = new XmlDocument();
            // Create Declaration node
            XmlNode xroot;
            xroot = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xdoc.AppendChild(xroot);
            xroot = xdoc.CreateNode(XmlNodeType.Element, "Pages", "");
            //xdoc.AppendChild(xroot);
            // Create URL node
            XmlNode xnode = xdoc.CreateNode(XmlNodeType.Element, "url", "");
            xnode.InnerText = _url;
            xroot.AppendChild(xnode);
            // Create URL NAME node
            xnode = xdoc.CreateNode(XmlNodeType.Element, "urlname", "");
            xnode.InnerText = _urlName;
            xroot.AppendChild(xnode);
            // Create URL NAME node
            xnode = xdoc.CreateNode(XmlNodeType.Element, "category", "");
            xnode.InnerText = CategoryID;
            xroot.AppendChild(xnode);
            // Create nodes contain Data from pattern
            if (string.IsNullOrEmpty(_pattern))
            {
                // Create CONTENT node
                xnode = xdoc.CreateNode(XmlNodeType.Element, "content", "");
                xnode.InnerText = _content;
                xroot.AppendChild(xnode);
            }
            else
            {
                //string Pattern = @"(?ixn-ms: (<a\s*href\s*=\s*""(?<href>[^""]+)""\s*>)(?<content>[^<]+)(</a>) )";

                    //System.Text.RegularExpressions.MatchCollection match = System.Text.RegularExpressions.Regex.Matches(Content, "(?i)href=[\"'](.+?)[\"']");
                System.Text.RegularExpressions.Regex Regex = new System.Text.RegularExpressions.Regex(_pattern);
                System.Text.RegularExpressions.MatchCollection match = System.Text.RegularExpressions.Regex.Matches(_content.Replace(Environment.NewLine, ""), @_pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);

                    foreach (System.Text.RegularExpressions.Match objMatch in match)
                    {
                        string[] groupNames = Regex.GetGroupNames();
                        for(int i = 0; i<objMatch.Groups.Count; i++)
                        {
                            try
                            {
                                int j = 0;
                                j = int.Parse(groupNames[i]);
                            }
                            catch
                            {
                                xnode = xdoc.CreateNode(XmlNodeType.Element, groupNames[i], "");
                                xnode.InnerText = objMatch.Groups[i].Value;
                                xroot.AppendChild(xnode);
                            }
                        }
                    }
            }

            xdoc.AppendChild(xroot);

            if (fPath == null || fPath.ToString() == "")
            {
                try
                {
                    System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "/files/" + rootURL);
                }
                catch { }
                xdoc.Save(AppDomain.CurrentDomain.BaseDirectory + "/files/" + rootURL + "/" + Guid.NewGuid() + ".xml");
            }
            else {
                if (!System.IO.Directory.Exists(fPath.ToString()))
                    System.IO.Directory.CreateDirectory(fPath.ToString());
                string p = fPath.ToString();
                xdoc.Save(fPath.ToString() + "/" + Guid.NewGuid() + ".xml"); 
            
            }
            
        }

       
    }
}
